package com.study.mike.common.converter;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.study.mike.common.enums.MagicalLandEnum;

/**
 * 自定义枚举转化器
 *
 * @author KangHui
 * @Date 2023/3/24 17:05
 */
public class MagicLandConverter implements Converter<MagicalLandEnum> {
    @Override
    public Class supportJavaTypeKey() {
        return MagicalLandEnum.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    /**
     * cellData 转java对象，导入时转换
     *
     * @param cellData
     * @param excelContentProperty
     * @param globalConfiguration
     * @return
     */
    @Override
    public MagicalLandEnum convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) {
        String stringValue = cellData.getStringValue();
        if (ObjectUtil.equal(stringValue, MagicalLandEnum.SINGLE.getValue())) {
            return MagicalLandEnum.SINGLE;
        } else if (ObjectUtil.equal(stringValue, MagicalLandEnum.TEAM.getValue())) {
            return MagicalLandEnum.TEAM;
        } else {
            return null;
        }
    }

    /**
     * 对象转cellDate,导出时转换
     *
     * @param magicalType
     * @param excelContentProperty
     * @param globalConfiguration
     * @return
     */
    @Override
    public WriteCellData<?> convertToExcelData(MagicalLandEnum magicalType, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) {
        if (ObjectUtil.equal(magicalType.getMagicalType(), MagicalLandEnum.SINGLE.getMagicalType())) {
            return new WriteCellData<>(MagicalLandEnum.SINGLE.getValue());
        } else if (ObjectUtil.equal(magicalType.getMagicalType(), MagicalLandEnum.TEAM.getMagicalType())) {
            return new WriteCellData<>(MagicalLandEnum.TEAM.getValue());
        } else {
            return null;
        }
    }

}
